package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domian.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 何须浅碧深红色
 * @date 2022/9/16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private CacheService cacheService;
    @Override
    public void keys(String key) {
        Integer count = 100; //count 限定调用次数可以从数据库中取数据也可以在代码中规定
        Integer limitTime = 60; //过期时间
        Integer redisCount = null;

        if (cacheService.exists(key)){
//            redisCount = cacheService.getExpire(key);
            if(redisCount >= count) {
                throw new CustomException("接口调用超出限定次数");

            }else {
                cacheService.incrBy(key,1);
            }
        }else {
            cacheService.set(key,"1");
        }
    }
}
